package com.ssyu.examination.system.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.mchange.lang.IntegerUtils;
import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.system.entity.ClazzEntity;
import com.ssyu.examination.system.entity.CourseEntity;
import com.ssyu.examination.system.service.ClazzService;

@WebServlet("/ClazzServlet")
public class ClazzServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private static ClazzService clazzService = new ClazzService();

	public ClazzServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("listClazz".equals(command)) {
			listClazz(request, response);
		} else if ("listMenu".equals(command)) {
			listMenu(request, response);
		} else if ("checkClassName".equals(command)) {
			checkClassName(request, response);
		} else if ("updateClazzState".equals(command)) {
			updateClazzState(request, response);
		} else if ("addClazz".equals(command)) {
			addClazz(request, response);
		} else if ("getclazz".equals(command)) {
			getclazz(request, response);
		} else if ("updateclazz".equals(command)) {
			updateclazz(request, response);
		}

	}

	/**
	 * 分页查询班级信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listClazz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String clazz = request.getParameter("clazz");
		String stateStr = request.getParameter("state");
		int state = 1;
		if (StringUtils.isNotBlank(stateStr)) {
			state = Integer.parseInt(stateStr);
		}
		PageModel<ClazzEntity> pageModel = clazzService.listClzz(page, rows,
				clazz, state);

		print(response, FastjsonUtil.toJsonWithDateFormat(pageModel,
				"yyyy-MM-dd HH:mm:ss"));
	}

	/**
	 * 生成课程树
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}

		List<CourseEntity> menuList = clazzService.listMenu(id);
		System.out.println(FastjsonUtil.toJson(menuList));
		print(response, FastjsonUtil.toJson(menuList));
		

	}

	/**
	 * 班名唯一验证
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void checkClassName(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String clazzName = request.getParameter("name");
		ClazzEntity clazzEntity = clazzService.checkClassName(clazzName);

		if (clazzEntity != null) {
			print(response, false);
		} else {
			print(response, true);
		}

	}

	/**
	 * 修改班级状态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void updateClazzState(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int states = Integer.parseInt(request.getParameter("state"));
		int state = 0;
		if (states == 1) {
			state = 2;
		} else if (states == 2) {
			state = 1;
		}

		int operateId = App.getOperateId(request, response);
		int row = clazzService.updateClazzState(id, state, operateId);

		if (row > 0) {
			print(response, 1);
		}

	}

	/**
	 * 班级新增
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addClazz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String clazzName = request.getParameter("name");
		int course = Integer.parseInt(request.getParameter("menu"));
		int operateId = App.getOperateId(request, response);
		int row = clazzService.clazzAdd(operateId, clazzName, course);

		if (row > 0) {
			print(response, 1);
		}

	}

	/**
	 * 获取指定班级信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void getclazz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		ClazzEntity clazzEntity = clazzService.getClazz(id);

		if (clazzEntity != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(clazzEntity,
					"yyyy-MM-dd HH:mm:ss"));
		}

	}

	/**
	 * 修改班级信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void updateclazz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String clazzName = request.getParameter("name");
		int state = Integer.parseInt(request.getParameter("state"));
		int course = Integer.parseInt(request.getParameter("menuUpdate"));
		int operateId = App.getOperateId(request, response);
		List<Object> list = new ArrayList<Object>();
		list.add(clazzName);
		list.add(operateId);
		list.add(state);
		list.add(course);
		list.add(id);

		int row = clazzService.updateClazz(list);

		if (row > 0) {
			print(response, 1);
		}
	}

}
